
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class LightSweep extends StatefulWidget {
  final String text;
  const LightSweep({super.key, required this.text});

  @override
  State<LightSweep> createState() => _LightSweepState();
}

class _LightSweepState extends State<LightSweep> with TickerProviderStateMixin {

  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 2500),
    );
    _animation = TweenSequence<double>([
      TweenSequenceItem<double>(
        tween: Tween<double>(begin: -0.5, end: 1.0)
            .chain(CurveTween(curve: Curves.linear)),
        weight: 0.6,
      ),
      TweenSequenceItem<double>(
        tween: Tween<double>(begin: 1.0, end: 1.0),
        weight: 0.4,
      ),
    ]).animate(_controller);

    _controller.repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    String content = widget.text;
    return Stack(
      children: [
        RepaintBoundary(
          child: Text(content, style: const TextStyle(color: Colors.black, fontSize: 100),),
        ),
        AnimatedBuilder(
          animation: _animation,
          builder: (context, child) {
            double current = _animation.value;
            double one = max(min(current + 0.4, 1.0), 0);
            double two = max(min(current + 0.4, 1.0), 0);
            double three = max(min(current + 0.5, 1.0), 0);
            double four = max(min(current + 0.6, 1.0), 0);
            double five = max(min(current + 0.6, 1.0), 0);
            return ShaderMask(
              shaderCallback: (Rect bounds) {
                return LinearGradient(colors: [
                  Colors.transparent,
                  Colors.white.withOpacity(0.3),
                  Colors.white.withOpacity(0.8),
                  Colors.white.withOpacity(0.3),
                  Colors.transparent,
                ],
                  begin: const Alignment(-1, -1),
                  end: const Alignment(1.0, 1.0),
                  stops: [one, two, three, four, five],
                ).createShader(bounds);
              },
              child: Text(content,style: const TextStyle(color: Colors.white, fontSize: 100),),
            );
          },
        ),
      ],
    );
  }
}
